package cn.zou.db;

import org.apache.log4j.Logger;
import org.junit.Test;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * Created by zou on 2017/7/10.
 * 数据库操作对象
 *
 */
public class DBOperation {
    Logger  logger= Logger.getLogger(DBOperation.class);
    //数据库连接回话
    private  Connection con =null;
    //获取DBmanager对象
    private DBManager dbManager=new DBManager();

    /**
     * 打开数据库连接会话
     * @return
     */
    public boolean open(){
        this.con =dbManager.getCon();
        if(this.con ==null){
            return false;
        }
        return true;
    }

    /**
     * 关闭会话
     */
    public void close(){
        try {
            if(this.con!=null){
                this.con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e);
        }
    }

    /**
     * 静态数据增删改
     * @return
     */
    public int executeUpdate(String sql){
        close();
        if(open()){
            try {
                Statement statement = this.con.createStatement();
                return statement.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
                logger.error(e);
            }
        }
        return -1;
    }
    public int executeUpdate(String sql,Map<Integer,Object> params){
        PreparedStatement preparedStatement= setPreparedStatement(sql,params);
        try {
            if(preparedStatement!=null){
                return preparedStatement.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e);
            return -1;
        }
        return -1;
    }
    /**
     *
     * @param sql 静态sql语句
     * @return 数据库查询数据
     */
    public ResultSet executeQuery(String sql){
        close();
        if(open()){
            try {
                Statement statement = this.con.createStatement();
                ResultSet resultSet = statement.executeQuery(sql);
                return resultSet;
            } catch (SQLException e) {
                e.printStackTrace();
                logger.error(e);
            }
        }
        return null;
    }
    public ResultSet executeQuery(String sql, Map<Integer,Object> params){

            try {
                PreparedStatement preparedStatement= setPreparedStatement(sql,params);
                if(preparedStatement!=null){
                    return preparedStatement.executeQuery();
                }else{
                    return null;
                }

            } catch (SQLException e) {
                e.printStackTrace();
                logger.error(e);
            }


        return null;
    }

    /**
     * 预编译sql
     * @param sql 带参数化得sql语句
     * @param params 参数
     * @return
     */
    private PreparedStatement setPreparedStatement(String sql, Map<Integer, Object> params) {
        close();
        if(open()){
            try {
                PreparedStatement preparedStatement = this.con.prepareStatement(sql);
                //设置输入参数 并且判断为何种类型
                for(int i=1;i<=params.size();i++){
                    //判断是否为字符串
                    if(params.get(i)==""){
                        preparedStatement.setString(i,"");
                    }else if(params.get(i).getClass()==Class.forName("java.lang.String")){
                        preparedStatement.setString(i,params.get(i).toString());
                    }else if(params.get(i).getClass()==Class.forName("java.lang.Integer")){
                        preparedStatement.setInt(i,(Integer)params.get(i));
                    }else if(params.get(i).getClass()==Class.forName("java.lang.Double")){
                        preparedStatement.setDouble(i,(Double)params.get(i));
                    }else if(params.get(i).getClass()==Class.forName("java.lang.Long")){
                        preparedStatement.setLong(i,(Long)params.get(i));
                    }else if(params.get(i).getClass()==Class.forName("java.lang.Float")){
                        preparedStatement.setFloat(i,(Float)params.get(i));
                    }else if(params.get(i).getClass()==Class.forName("java.lang.Boolean")){
                        preparedStatement.setBoolean(i,(Boolean)params.get(i));
                    }else if(params.get(i).getClass()==Class.forName("java.util.Date")){
                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String valuefo=simpleDateFormat.format((Date)params.get(i));
                        preparedStatement.setString(i,valuefo);
                    }
                }
                return preparedStatement;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
    @Test
    public void testOperation(){
        DBOperation dbp=new DBOperation();
        HashMap<Integer,Object> parms=new HashMap<>();
        parms.put(1,"1");
        ResultSet resultSet = dbp.executeQuery("SELECT * FROM user WHERE ?",parms);

    }
}
